package multiThread.ThreadUtilPackage;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

    //CountDownLatch的构造函数接收一个int类型的参数作为计数器，如果你想等待N个点完成，这里就传入N。
    static CountDownLatch c = new CountDownLatch(2);
    public static void main(String[] args) throws InterruptedException{
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(1+":"+Thread.currentThread().getName());
                //当我们调用CountDownLatch的countDown方法时，N就会减1，
                c.countDown();
                System.out.println(2+":"+Thread.currentThread().getName());
                c.countDown();
            }
        }).start();
        // CountDownLatch的await方法会阻塞当前线程，直到N变成零。
        //如果countDownLatch的值不为0 则阻塞主线程
        c.await();
        System.out.println(3+":"+Thread.currentThread().getName());
    }
}
